var express = require('express');
var router = express.Router();
var r = require('../biz/users');

/* GET users listing. */
router.get('/login', function(req, res, next) {
  r.login(req,res)
});

router.post('/regist', function(req, res, next) {
    r.regist(req,res)
});
router.get('/getUserInfo', function(req, res, next) {
    r.getUserInfo(req,res)
});

var svgCaptcha = require('svg-captcha');

router.get('/getCaptcha', function(req, res, next) {
    console.log(`当前用户为：`+JSON.stringify(req.headers.session))
    //创建验证码图片
    var captcha = svgCaptcha.create({
        // 翻转颜色
        inverse: false,
        // 字体大小
        fontSize: 36,
        // 噪声线条数
        noise: 2,
        // 宽度
        width: 80,
        // 高度
        height: 30,
    });
    // 保存到session,忽略大小写
    req.session.captcha = captcha.text.toLowerCase();
    console.log(req.session.captcha); //0xtg 生成的验证码
    res.setHeader('Content-Type', 'image/svg+xml');
    res.write(String(captcha.data));
    res.end()
})

router.get('/captchaValid',(req,resp) =>{
    let captcha =req.query.captcha;
    console.log(captcha);
    if (captcha ==  req.session.captcha) {
        resp.send()
    }else{
        throw new Error('验证失败')
    }
})
module.exports = router;
